package com.example.glgk.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lxk
 */
public class DateUtils {

    public static String getMomTime(String time){
        try {
            SimpleDateFormat dateFormat;

            if (time.length() == 4) {
                dateFormat = new SimpleDateFormat("yyyy");
            } else if (time.length() == 7) {
                dateFormat = new SimpleDateFormat("yyyy-MM");
            }else if (time.length() == 10){
                dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }
            else {
                throw new IllegalArgumentException("Invalid time format. Supported formats are 'yyyy' or 'yyyy-MM'.");
            }

            Date inputDate = dateFormat.parse(time);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(inputDate);

            calendar.add(Calendar.MONTH, -1);

            String momTime = dateFormat.format(calendar.getTime());

            return momTime;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getYoyTime(String time){
        try {
            SimpleDateFormat dateFormat;

            if (time.length() == 4) {
                dateFormat = new SimpleDateFormat("yyyy");
            } else if (time.length() == 7) {
                dateFormat = new SimpleDateFormat("yyyy-MM");
            } else if (time.length() == 10){
                dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }else {
                throw new IllegalArgumentException("Invalid time format. Supported formats are 'yyyy' or 'yyyy-MM'.");
            }

            Date inputDate = dateFormat.parse(time);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(inputDate);

            calendar.add(Calendar.YEAR, -1);

            String momTime = dateFormat.format(calendar.getTime());

            return momTime;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}
